﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SAS.Toolkit;
using System.Text.RegularExpressions;
/// <summary>
///basePage 的摘要说明
/// </summary>
public class basePage : System.Web.UI.Page
{
    /// <summary>
    /// 天狼星团队实体
    /// </summary>
    protected TeamInfos teamInfos = new TeamInfos();
    /// <summary>
    /// 团队成员实体
    /// </summary>
    protected MemberInfo minfo = new MemberInfo();
	public basePage()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
        teamInfos = SASAPIHelper.GetTeamInfo(1);
	}
    /// <summary>
    /// 获取缓存里团队成员列表
    /// </summary>
    /// <param name="tid"></param>
    /// <returns></returns>
    protected MemberInfo[] GetCacheMembers(int tid)
    {
        MemberInfo[] minfos = System.Web.HttpRuntime.Cache.Get("member_" + tid) as MemberInfo[];
        if (minfos == null)
        {
            minfos = SASAPIHelper.GetMembers(tid);
            if (minfos != null)
            {
                System.Web.HttpRuntime.Cache.Insert("member_" + tid, minfos, null, DateTime.Now.AddMinutes(30), System.Web.Caching.Cache.NoSlidingExpiration);
            }
        }
        return minfos;
    }
    /// <summary>
    /// 获取缓存里活动列表
    /// </summary>
    /// <param name="tid"></param>
    /// <returns></returns>
    protected TeamActInfos[] GetCacheTeamActs(int tid)
    {
        TeamActInfos[] activs = System.Web.HttpRuntime.Cache.Get("activity_" + tid) as TeamActInfos[];
        if (activs == null)
        {
            activs = SASAPIHelper.GetTeamActList(tid);
            if (activs != null)
            {
                System.Web.HttpRuntime.Cache.Insert("activity_" + tid, activs, null, DateTime.Now.AddMinutes(30), System.Web.Caching.Cache.NoSlidingExpiration);
            }
        }
        return activs;
    }
    /// <summary>
    /// 获取缓存里团队成果列表
    /// </summary>
    /// <param name="tid"></param>
    /// <returns></returns>
    protected TeamWorkInfos[] GetCacheTeamWorkInfos(int tid)
    {
        TeamWorkInfos[] workInfo = System.Web.HttpRuntime.Cache.Get("workInfo_" + tid) as TeamWorkInfos[];
        if (workInfo == null)
        {
            workInfo = SASAPIHelper.GetTeamWorkList(tid);
            if (workInfo != null)
            {
                System.Web.HttpRuntime.Cache.Insert("workInfo_" + tid, workInfo, null, DateTime.Now.AddMinutes(30), System.Web.Caching.Cache.NoSlidingExpiration);
            }
        }
        return workInfo;
    }
    /// <summary>
    /// 去除img标签,保留其余
    /// </summary>
    /// <param name="HTMLStr"></param>
    /// <returns></returns>
    protected string removeImg(string HTMLStr)
    {
        return Regex.Replace(HTMLStr, @"<IMG.*?src=(['""]?)(?<url>[^'"" ]+)(?=\1)[^>]*>", delegate(Match m) { return ""; });
    }
    /// <summary>
    /// 去除html标签
    /// </summary>
    /// <param name="HTMLStr"></param>
    /// <returns></returns>
    protected string removeHtml(string HTMLStr)
    {
        return Regex.Replace(HTMLStr, "<[^>]*>", string.Empty, RegexOptions.IgnoreCase);
    }
    /// <summary>
    /// 获取字符串中img标签中src的数组列表
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    protected string[] GetImgSrcList(string str)
    {
        string[] strList=null;
        string regStr = "\\<IMG\\ [\\s\\S]*?src=['\"]?(?<p>[^'\"\\>\\ ]+)['\"\\>\\ ]";
        string cont1 = string.Empty; //图片的src
        Regex reg = new Regex(regStr, RegexOptions.Compiled | RegexOptions.IgnoreCase);
        Match match = reg.Match(str);
        string picSrc = "";
        while (match.Success)
        {
            picSrc = match.Groups["p"].Value;
            cont1 += string.Format("{0}", picSrc + ",");
            match = match.NextMatch();
        }
        if (cont1 != "")
        {
            cont1 = cont1.Substring(0, cont1.LastIndexOf(','));
            strList = cont1.Split(',');
        }  
            return strList;
    }
    /// <summary>
    /// 根据用户名获取缓存里的用户实体
    /// </summary>
    /// <param name="userName"></param>
    /// <returns></returns>
    protected MemberInfo GetCacheMemberInfo(string userName)
    {
        MemberInfo[] minfos = GetCacheMembers(1);
        List<MemberInfo> mlist = new List<MemberInfo>();
        mlist.AddRange(minfos);
        //委托
        Predicate<MemberInfo> mpush = new Predicate<MemberInfo>(delegate(MemberInfo info) { return info.Name == userName; });
        minfo = mlist.Find(mpush);
        return minfo;
    }
}
